package com.acme.bankapp.domain.bank;

public abstract class AbstractAccount implements Account {
	private static final long serialVersionUID = 3331164492605524416L;
	protected final String NEGATIVE_VALUE = "Amount cant be negative";
	protected final String NOT_ENOUGH_FOUNDS = "Not enough founds";
	protected int id;
	protected double balance;

	public AbstractAccount(final double amount) throws NegativeArgumentValueException {
		if (amount < 0)
			throw new NegativeArgumentValueException(NEGATIVE_VALUE);
		this.balance = amount;
	}

	public double getBalance() {
		return balance;
	}

	protected void setBalance(final double balance) {
		this.balance = balance;
	}

	public void deposit(final double amount) throws NegativeArgumentValueException {
		if (amount < 0) {
			throw new NegativeArgumentValueException(NEGATIVE_VALUE);
		}
		setBalance(balance + amount);
	}
	
	public long decimalValue(){
		return Math.round(balance);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractAccount other = (AbstractAccount) obj;
		if (Double.doubleToLongBits(balance) != Double.doubleToLongBits(other.balance))
			return false;
		if (id != other.id)
			return false;
		return true;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Account id: ").append(id).append("; Balance: ").append(balance);
		return sb.toString();
	}
}
